package com.itstherules.jmsunit.queuemanager;

import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.QueueConnection;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;

import com.itstherules.jmsunit.managers.QueueManager;

public abstract class BaseQueueManager implements QueueManager {

	protected QueueSession session;

	protected QueueConnection connection;
	
	public void close() throws JMSException {
		connection.close();
	}

	public TextMessage createTextMessage(String message) throws JMSException {
		return session.createTextMessage(message);
	}

	public Queue createQueue(String queueName) throws JMSException {
		return session.createQueue(queueName);
	}

	public QueueSender getQueueSender(String queue) throws JMSException {
		return session.createSender(this.createQueue(queue));
	}

	public QueueReceiver getQueueReceiver(String queue) throws JMSException {
		return session.createReceiver(this.createQueue(queue));
	}
	
	public abstract MessageConsumer getMessageConsumerWithMessageID(String queue, String messageId) throws JMSException;

	public QueueBrowser getQueueBrowser(String queue) throws JMSException {
		return session.createBrowser(this.createQueue(queue));
	}
	
	public TextMessage getTextMessage(String message) {
		TextMessage textMessage = null;
		try {
			textMessage = createTextMessage(message);
		} catch (JMSException e) {
			throw new RuntimeException("Programmatic error", e);
		}
		return textMessage;
	}
	
}
